package com.michaelwolf.mytool.wcfcompmanger.ui;

import java.awt.Color;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringWriter;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Node;

import com.michaelwolf.mytool.util.StringUtils;
import com.michaelwolf.mytool.wcfcompmanger.wcfanalyzer.WCFLoader;

public class NodeInfoDisplayFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8277357118502509148L;
	private JTextPane textPane = new JTextPane();
	private JScrollPane mainPanel = new JScrollPane(textPane);

	// public NodeInfoDisplayFrame(String referredID, String nodeContent) throws
	// BadLocationException{
	// super(referredID);
	// setSize(1000, 800);
	//
	// mainPanel.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
	// mainPanel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
	//
	// add(mainPanel);
	//
	// mainPanel.setBounds(10, 20, 960, 700);
	// textPane.setText(nodeContent);
	//
	//
	// Document document = textPane.getDocument();
	//
	// StyleContext sc = StyleContext.getDefaultStyleContext();
	// AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY,
	// StyleConstants.Foreground, Color.RED);
	//
	// int index = 0;
	// String nodeContentWithoutSpace = StringUtils.replaceTab(nodeContent);
	//
	// while(nodeContentWithoutSpace.indexOf(referredID, index) != -1){
	// index = nodeContentWithoutSpace.indexOf(referredID, index);
	// int start = index;
	// String referIDInNodeStr = document.getText(start, referredID.length());
	// document.remove(start, referredID.length());
	// document.insertString(start, referIDInNodeStr, aset);
	//
	// index += referredID.length();
	//
	// }
	//
	// setVisible(true);
	//
	// }
	
	public NodeInfoDisplayFrame(FileNodeObject fileNodeObject) throws IOException {
		String keywork = fileNodeObject.getKeyword();
		File scriptFile = fileNodeObject.getScriptFile();
		BufferedReader bf = new BufferedReader(new FileReader(scriptFile));

		String str = "";
		StringBuilder sb = new StringBuilder();

		while (str != null) {
			str = bf.readLine();

			if (str == null) {
				break;
			}

			sb.append(str + "\n");
		}

		bf.close();
		showContentOnFrame(keywork, sb.toString());
		setVisible(true);
	}

	public NodeInfoDisplayFrame(WCFNodeObject userObject) {
		Node compNode = userObject.getNode();
		if (isScriptNode(compNode)) {
			try {
				displayScriptFileContent(userObject);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			displayNodeContent(userObject);
		}

		setVisible(true);

	}

	private void displayScriptFileContent(WCFNodeObject userObject) throws IOException
			 {
		String keywork = userObject.getSearchWord();
		Node compNode = userObject.getNode();
		String moduleName = userObject.getModuleName();
		
		String scriptId = compNode.getAttributes().getNamedItem("id")
				.getNodeValue();
		
		File scriptFile = new File(WCFLoader.WCFDIR + moduleName + "\\scripts\\"
				+ scriptId + ".groovy");
		BufferedReader bf = new BufferedReader(new FileReader(scriptFile));

		String str = "";
		StringBuilder sb = new StringBuilder();

		while (str != null) {
			str = bf.readLine();

			if (str == null) {
				break;
			}

			sb.append(str + "\n");
		}

		bf.close();
		showContentOnFrame(keywork, sb.toString());
	}

	private boolean isScriptNode(Node compNode) {
		return "script-function".equals(compNode.getNodeName());
	}

	private void displayNodeContent(WCFNodeObject userObject) {
		String content = "";
		String keyword = userObject.getSearchWord();
		Node compNode = userObject.getNode();
		
		String moduleDisplayName = compNode.getOwnerDocument().getFirstChild()
				.getAttributes().getNamedItem("display-name").getNodeValue();

		this.setTitle("Module:" + moduleDisplayName + " keyword=" + keyword);
		Transformer transformer;
		try {
			transformer = TransformerFactory.newInstance().newTransformer();
			StringWriter sw = new StringWriter();
			transformer
					.transform(new DOMSource(compNode), new StreamResult(sw));
			content = sw.toString();

		} catch (TransformerConfigurationException
				| TransformerFactoryConfigurationError e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TransformerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		showContentOnFrame(keyword, content);
	}

	private void showContentOnFrame(String keywork, String content) {
		setSize(1000, 800);

		mainPanel
				.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		mainPanel
				.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

		add(mainPanel);

		mainPanel.setBounds(10, 20, 960, 700);
		textPane.setText(content);

		Document document = textPane.getDocument();

		StyleContext sc = StyleContext.getDefaultStyleContext();
		AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY,
				StyleConstants.Foreground, Color.RED);

		int index = 0;
		String nodeContentWithoutSpace = StringUtils.replaceTab(content);

		while (nodeContentWithoutSpace.indexOf(keywork, index) != -1) {
			index = nodeContentWithoutSpace.indexOf(keywork, index);
			int start = index;
			String referIDInNodeStr;
			try {
				referIDInNodeStr = document.getText(start, keywork.length());
				document.remove(start, keywork.length());
				document.insertString(start, referIDInNodeStr, aset);
			} catch (BadLocationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			index += keywork.length();

		}
	}

}
